package com.sevennotes.classtable.ui.pages.components

import androidx.compose.foundation.*
import androidx.compose.foundation.layout.*
import androidx.compose.material.Divider
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.zIndex
import com.sevennotes.classtable.config.Config
import com.sevennotes.classtable.config.Settings
import com.sevennotes.classtable.model.ClassItemWithName
import com.sevennotes.classtable.ui.pages.main_screen.ClassItemState
import com.sevennotes.classtable.ui.pages.main_screen.MainScreenState
import com.sevennotes.classtable.utils.TimeUtil


@Composable
fun TableBox(
  mainScreenState: MainScreenState,
  hScrollState: ScrollState = rememberScrollState(),
  onClassItemMoved: (ClassItemState, Int, Int) -> Unit,
  onClassItemTapped: (ClassItemWithName) -> Unit,
  onClassItemDrag: (ClassItemState, Int, Int) -> Unit
) {
  val classList by mainScreenState.classList!!.observeAsState()

  Box(
    modifier = Modifier
      .verticalScroll(rememberScrollState())
      .height(IntrinsicSize.Max)
  ) {
    TimeColumnDivider(
      beginHour = Settings.beginHour,
      endHour = Settings.endHour,
      height = mainScreenState.oneHourHeight.dp
    )
    Row(
      modifier = Modifier
        .fillMaxHeight()
        .horizontalScroll(hScrollState)
        .zIndex(1f)
    ) {
      Spacer(modifier = Modifier.width(20.dp))
      repeat(7) { weekDay ->
        Divider(
          modifier = Modifier
            .fillMaxHeight()
            .zIndex(0.9f)
            .width(1.dp),
          color = Config.dividerColor
        )
        val stuList: List<ClassItemState> = if (classList != null) {
          classList!!.filter { it.classItem.weekDay == weekDay }
            .map {
              ClassItemState(
                item = it,
                positionY = mainScreenState.calculatePositionY(it.classItem.startTime),
                classHeight = mainScreenState.calculateClassHeight(it.classItem.duration)
              )
            }
        } else {
          listOf<ClassItemState>()
        }

        OneDayColumn(
          modifier = Modifier
            .width(mainScreenState.oneDayWidth.dp)
            .zIndex(1f),
          isToday = weekDay == TimeUtil.currentWeekDay(),
          studentList =  stuList,
          onClassItemMoved = { classItem, offsetX, offsetY ->
            onClassItemMoved(classItem, offsetX, offsetY)
          },
          onClick = onClassItemTapped,
          onDrag = onClassItemDrag
        )
      }
    }
    TimeColumnText(
      beginHour = Settings.beginHour,
      endHour = Settings.endHour,
      height = mainScreenState.oneHourHeight.dp
    )
  }
}


@Composable
fun OneDayColumn(
  modifier: Modifier = Modifier,
  isToday: Boolean,
  studentList: List<ClassItemState>,
  onClassItemMoved: (ClassItemState, Int, Int) -> Unit,
  onClick: (ClassItemWithName) -> Unit,
  onDrag: (ClassItemState, Int, Int) -> Unit
) {
  var zIndex by remember { mutableStateOf(1f) }
  fun isToday(yes: Boolean) = if (yes) modifier.background(color = Config.currentDay) else modifier
  Box(modifier = isToday(isToday).fillMaxHeight().zIndex(zIndex)) {
    studentList.forEach { item ->
      StudentButton(
        classItem = item,
        onClassItemMoved = { classItem, offsetX, offsetY ->
          zIndex = 1f
          onClassItemMoved(classItem, offsetX, offsetY)
        },
        onDrag = {classItem, x, y ->
          zIndex = 3.0f
          onDrag(classItem, x, y)
        },
        onClick = { onClick(it) }
      )
    }
  }
}

@Composable
fun TimeColumnText(
  beginHour: Int,
  endHour: Int,
  height: Dp
) {
  Column(modifier = Modifier.zIndex(1.1f)) {
    repeat(endHour + 1 - beginHour) {
      Spacer(
        modifier = Modifier
          .height(1.dp),
      )
      Text(
        text = "${it + beginHour}",
        modifier = Modifier
          .height(height)
          .width(20.dp)
          .zIndex(2f)
      )
    }
    Spacer(
      modifier = Modifier
        .height(1.dp),
    )
  }
}


@Composable
fun TimeColumnDivider(
  beginHour: Int,
  endHour: Int,
  height: Dp
) {
  Column {
    repeat(endHour + 1 - beginHour) {
      Divider(
        modifier = Modifier
          .fillMaxWidth()
          .zIndex(0.9f)
          .height(1.dp),
        color = Config.dividerColor
      )
      Spacer(
        modifier = Modifier
          .height(height)
      )
    }
    Divider(
      modifier = Modifier
        .fillMaxWidth()
        .zIndex(0.9f)
        .height(1.dp),
      color = Config.dividerColor
    )
  }
}
